<div class="col-mg-12" style="text-align: center;">
    <h3>文章列表</h3>
</div>

<div class="col-md-12 tabs-div">
    <ul class="nav nav-tabs">
        <li role="presentation" <?php if (empty($_params['type'])) { echo 'class="active"'; }?>><a href="/article/listp">全部</a></li>
        <li role="presentation" <?php if (($_params['type'] == 1)) { echo 'class="active"'; }?>><a href="/article/listp?type=1">PHPer</a></li>
        <li role="presentation" <?php if (($_params['type'] == 2)) { echo 'class="active"'; }?>><a href="/article/listp?type=2">Think</a></li>
        <li role="presentation" <?php if (($_params['type'] == 3)) { echo 'class="active"'; }?>><a href="/article/listp?type=3">GoodLife</a></li>
        <!--
        <li role="presentation"><a href="/sys/menu/power">目录权限</a></li>
        -->
    </ul>
</div>
<div class="col-sm-12">
    <form action="/sys/menu" method="get">
        <div class="col-md-5">
            <div class="form-group col-md-10">
                <div class="input-group">
                    <div class="input-group-addon">文章标题</div>
                    <input type="text" class="form-control col-md-7" name="title" id="" placeholder="请输入文章标题" value="<?= !empty($_params['menu_title']) ? $_params['menu_title'] : '' ?>" >
                </div>
            </div>
        </div>

        <div class="col-md-1" >
            <div class="form-group">
                <input type="submit" class="btn btn-sm btn-info" value="搜索">
            </div>
        </div>
        <div class="col-md-1" style="text-align: right;">
            <!--<a onclick="downloadfile()" class="btn btn-sm btn-info">导出表格</a>-->
        </div>
    </form>
</div>

<table class="table table-hover tableWithBorder">
    <thead>
    <tr>
        <th>文章ID</th>
        <th>类型</th>
        <th>标题</th>
        <th>发布日期</th>
        <th>状态</th>
        <th>操作</th>
    </tr>
    </thead>
    <tbody>
    <?php
    foreach ($list as $article) {
        ?>
        <tr class="level-father">
            <td><?= $article['id'] ?></td>
            <td><?= $article['type_text'] ?></td>
            <td><?= $article['title'] ?: '无' ?></td>
            <td><?= $article['publish_time'] ?></td>
            <td><?= $article['status_text'] ?></td>
            <td>
                <a class="btn btn-default btn-sm btn-info" href="/article/edit?id=<?=$article['id']?>">编辑</a>
                <button class="btn btn-default btn-sm btn-info publish" data-id="<?= $article['id'] ?>">
                    <?= $article['status'] == 1 ? "隐藏" : "发布"?>
                </button>
                <?php if ($article['is_index'] != 1) { ?>
                    <button class="btn btn-default btn-sm btn-info index" data-id="<?= $article['id'] ?>">
                        上首页
                    </button>
                <?php } ?>

            </td>
        </tr>
        <?php
    }
    ?>
    </tbody>
</table>

<script>
    var ajaxUrl = '/article/ajax';
    $(function() {
        $(".publish").click(function(){
            var articleId      = $(this).data('id');

            var postData = {
                action  : 'publish',
                id : articleId,
            };

            $.ajax({
                type: "POST",
                url: ajaxUrl,
                data: postData,
                dataType: "json",
                success:function(resp) {
                    console.log(resp);
                    if(resp.status == 200) {
                        alert("操作成功");
                        window.location.reload();
                    } else {
                        alert(resp.msg);
                        window.location.reload();
                    }
                }
            });
        });

        $(".index").click(function(){
            var articleId      = $(this).data('id');

            var postData = {
                action  : 'index',
                id : articleId,
            };

            $.ajax({
                type: "POST",
                url: ajaxUrl,
                data: postData,
                dataType: "json",
                success:function(resp) {
                    console.log(resp);
                    if(resp.status == 200) {
                        alert("操作成功");
                        window.location.reload();
                    } else {
                        alert(resp.msg);
                        window.location.reload();
                    }
                }
            });
        });
    });
</script>